package com.reggielcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggielcy.common.CustomException;
import com.reggielcy.entity.Category;
import com.reggielcy.entity.Dish;
import com.reggielcy.entity.Setmeal;
import com.reggielcy.mapper.CategoryMapper;
import com.reggielcy.service.CategoryService;
import com.reggielcy.service.DishService;
import com.reggielcy.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * @auther teacher
 * @date 2024/3/28
 * @project reggie_take_out
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long ids) {
        //如果关联了菜品，则抛出自定义异常
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,ids);
        int count = dishService.count(queryWrapper);
        if (count>0){
            //抛出异常
            throw new CustomException("当前分类已关联菜品,不能删除");
        }
        //如果关联了套餐，则抛出自定义异常
        LambdaQueryWrapper<Setmeal> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,ids);
        int count1 = setmealService.count(queryWrapper1);
        if (count1>0){
            //抛出异常
            throw new CustomException("当前分类已关联套餐,不能删除");
        }
        //删除
        super.removeById(ids);
    }
}
